package vn.edu.uit.chemistry;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

public class NhanFile extends Activity {

	TextView tvkind;
	ImageView imgView;
	Button btnBack;

	TextView tvFile;
	TextView tvSize;
	TextView tvSender;

	TextView tvPercent;
	ProgressBar prPercent;

	String kind = "";
	String nameFile = "";
	String sizeFile = "";
	String sender = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.screen_nhan_file);

		tvkind = (TextView) findViewById(R.id.tvTitle);
		SharedPreferences prefs = getSharedPreferences("FILENHAN", 0);
		kind = prefs.getString("kind", "");
		nameFile = prefs.getString("name", "");
		sizeFile = prefs.getString("size", "");
		sender = prefs.getString("sender", "");

		if (kind.equals("BKT15")) {
			tvkind.setText("Nhận bài kiểm tra 15'");
		} else if (kind.equals("BKT45")) {
			tvkind.setText("Nhận bài kiểm tra 45'");
		} else if (kind.equals("TLGV")) {
			tvkind.setText("Nhận tài liệu từ giáo viên");
		} else if (kind.equals("TLBB")) {
			tvkind.setText("Nhận tài liệu từ bạn bè");
		} else if (kind.equals("DABCGV")) {
			tvkind.setText("Đáp án bài cũ từ giáo viên");
		} else if (kind.equals("DABCBB")) {
			tvkind.setText("Tham khảo bài cũ từ bạn bè");
		} else if (kind.equals("BKGV")) {
			tvkind.setText("Nhận tài liệu tham khảo");
		} else if (kind.equals("BKBB")) {
			tvkind.setText("Nhận tài liệu tham khảo");
		}
		
		btnBack = (Button) findViewById(R.id.btnBackNhanFile);
		btnBack.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				finish();
			}
		});

		imgView = (ImageView) findViewById(R.id.imgNhanFile);
		imgView.postDelayed(onEverySecond, 0);

		tvFile = (TextView) findViewById(R.id.tvFile);
		tvSize = (TextView) findViewById(R.id.tvSize);
		tvSender = (TextView) findViewById(R.id.tvSender);

		tvFile.setText(nameFile);
		tvSize.setText(sizeFile + " bytes");
		tvSender.setText(sender);

		tvPercent = (TextView) findViewById(R.id.tvPercent);
		tvPercent.setText("0 %");

		prPercent = (ProgressBar) findViewById(R.id.prPercent);
		prPercent.setProgress(0);

	}

	public Runnable onEverySecond = new Runnable() {
		public void run() {
			new UpdatePrecentAsyncTask(getApplicationContext()).execute();
		}
	};

	final class UpdatePrecentAsyncTask extends AsyncTask<Void, Void, Void> {

		private Context context;
		private int percent = 0;

		public UpdatePrecentAsyncTask(Context ctx) {
			this.context = ctx;
		}

		@Override
		protected Void doInBackground(Void... param) {
			SharedPreferences prefs = context.getSharedPreferences("FILENHAN",
					0);
			percent = prefs.getInt("percent", 0);
			return null;
		}

		@Override
		protected void onPostExecute(final Void unused) {
			tvPercent.setText(percent + " %");
			prPercent.setProgress(percent);
			if (percent < 100) {
				imgView.postDelayed(onEverySecond, 0);
				btnBack.setVisibility(View.GONE);
			} else {
				imgView.removeCallbacks(onEverySecond);
				if (kind.equals("BKT15")) {
					finish();
					Intent intent = new Intent(NhanFile.this, LamBaiKT15.class);
					startActivity(intent);
				} else if (kind.equals("BKT45")) {
					finish();
					Intent intent = new Intent(NhanFile.this, LamBaiKT45.class);
					startActivity(intent);
				}
				
				btnBack.setVisibility(View.VISIBLE);
				//finish();
			}

		}

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			return false;
		}
		return false;
	}
}
